﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using VersionComparer.Base;
using VersionComparer.DataModel.Assemblies.Images;

namespace VersionComparer.DataModel.Assemblies
{
    [Serializable]
    [Order(7)]
    public class FieldItem: AssemblyItemBase<FieldInfo>
    {
        RtfBuilder rawValueRtf = new RtfBuilder ();

        protected override Type AssociatedType(FieldInfo value)
        {
            return value.FieldType;
        }
        protected override void Initialize(FieldInfo value)
        {
            base.Initialize(value);
            if (value.IsLiteral)
            {
                rawValueRtf.Append(" = ");

                BuildValueRtf(value.FieldType, value.GetRawConstantValue(), rawValueRtf);
            }
            IsPublic = value.IsPublic;
            IsConst = value.IsLiteral;
            IsStatic = value.IsStatic && !value.IsLiteral;
            IsAssembly = value.IsAssembly;
            IsFamilyOrAssembly = value.IsFamilyOrAssembly;
            IsFamily = value.IsFamily;
            
            Name = value.Name;
            Text = Name;
            
        }


        protected override CustomAttributeData[] GetAttributes(FieldInfo value)
        {
            return CustomAttributeData.GetCustomAttributes(value).ToArray();
        }
        
        protected override string GetFullText(FieldInfo value)
        {
            return string.Format("{2}{0}{1};", AddSpaceIfNotEmpty(UnderlyingTypeRtf.Text), Name, AccessorsAsCode);
        }


        protected override string AccessorsAsCode
        {
            get
            {
                string accessorsAsCode = base.AccessorsAsCode;
                if (IsConst)
                    return string.Format("{0}{1}", accessorsAsCode, "const");
                else
                    return accessorsAsCode;
            }
        }
        protected override void BuildRtf(RtfBuilder builder, bool differencesOnly, bool useParentColor)
        {
            BuildAttributesRtf(builder, differencesOnly, useParentColor);
            builder.Append(AddSpaceIfNotEmpty( AccessorsAsCode ), keywordColor);
            builder.Append(UnderlyingTypeRtf);
            builder.Append( " " + Name);
            builder.Append ( rawValueRtf );
            builder.AppendLine ( ";");
            
        }

        protected override System.Drawing.Image GetSpecificImage()
        {
            if ( IsConst )
                return AssemblyResources._const;
            else
                return AssemblyResources.field;
        }


        
    }
}
